package com.ysx.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ysx.server.mapper.CommentMapper;
import com.ysx.server.mapper.GoodsMapper;
import com.ysx.server.mapper.ReportMapper;
import com.ysx.server.mapper.UserMapper;
import com.ysx.server.pojo.dto.*;
import com.ysx.server.pojo.entity.Comment;
import com.ysx.server.pojo.entity.Goods;
import com.ysx.server.pojo.entity.Report;
import com.ysx.server.pojo.entity.User;
import com.ysx.server.pojo.vo.UserInfoVO;
import com.ysx.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.ysx.server.common.UpdateContent.USERNAME_UPDATE;
import static com.ysx.server.common.content.CAPTCHA_SESSION;


@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ReportMapper reportMapper;

    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @Override
    public boolean register(UserRegisterDTO userRegisterDTO, HttpServletRequest request) {
        String userAccount=userRegisterDTO.getUserAccount();
        String userPassword = userRegisterDTO.getUserPassword();
        String checkPassword = userRegisterDTO.getCheckPassword();
        String captcha = userRegisterDTO.getCaptcha();
        //1. 参数不能为空
        if(StringUtils.isAllBlank(userAccount,userPassword,checkPassword,captcha)){
            //TODO
            return false;
        }
        //账户不能小于5位
        if(userAccount.length()<5){
            //TODO
            return false;
        }
        //密码不能小于6位
        if(userPassword.length()<6&&checkPassword.length()<6){
            //TODO
            return false;
        }
        //二次密码和密码相同
        if(!userPassword.equals(checkPassword)){
            //TODO
            return false;
        }
        //验证码正确
        String captcha_session=(String) request.getSession().getAttribute(CAPTCHA_SESSION);
        if(!captcha.equals(captcha_session)){
            //TODO
            return false;
        }
        //2. 账户不能重复
        User user=userMapper.getByAccount(userAccount);
        if(user!=null){
            //TODO
            return false;
        }
        //3. 插入新用户
        userPassword= DigestUtils.md5DigestAsHex(userPassword.getBytes());
        userMapper.save(userAccount,userPassword);
        return true;
    }

    /**
     * 登陆
     *
     * @param userLoginDTO
     * @return
     */
    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String userAccount = userLoginDTO.getUserAccount();
        String userPassword = userLoginDTO.getUserPassword();
        //1. 参数不能为空
        if(StringUtils.isAllBlank(userAccount,userPassword)){
            return null;
        }
        //账户不能小于5位
        if(userAccount.length()<5){
            //TODO
            return null;
        }
        //密码不能小于6位
        if(userPassword.length()<6){
            //TODO
            return null;
        }
        //2. 查找用户
        User user=userMapper.getByAccount(userAccount);
        if(user==null){
            //TODO
            return null;
        }
        userPassword= DigestUtils.md5DigestAsHex(userPassword.getBytes());
        if(!userPassword.equals(user.getUserPassword())){
            return null;
        }
        return user;
    }

    /**
     * 管理员获取用户信息
     *
     * @return
     * @param userPageQueryDTO
     */
    @Override
    public Page<UserInfoVO> getUserManage(UserPageQueryDTO userPageQueryDTO) {
        PageHelper.startPage(userPageQueryDTO.getCurrent(),userPageQueryDTO.getPageSize());
        return userMapper.getUserManage(userPageQueryDTO);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        if(id==null||id<1){
            return false;
        }
        return userMapper.delete(id);
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public boolean save(User user) {
        if(user==null){
            return false;
        }
        if(user.getUserPassword()==null||"".equals(user.getUserPassword())){
            String password= DigestUtils.md5DigestAsHex("1234567".getBytes());
            user.setUserPassword(password);
        }
        return userMapper.saveUser(user);
    }

    /**
     * 修改用户
     *
     * @param userDTO
     * @return
     */
    @Override
    @Transactional
    public boolean update(UserDTO userDTO) {
        if(userDTO==null){
            return false;
        }
        if(userDTO.getUpdateEnum().equals(USERNAME_UPDATE)){
            //1. 如果用户名发生变化 则修改商品信息
            List<Goods> goodsList=goodsMapper.getByUserId(userDTO.getId());
            if(goodsList!=null&&goodsList.size()>0){
                goodsList.forEach(goods -> {
                    goods.setUsername(userDTO.getUsername());
                    GoodsDTO goodsDTO=new GoodsDTO();
                    BeanUtils.copyProperties(goods,goodsDTO);
                    goodsMapper.update(goodsDTO);
                });
            }
            //2. 如果用户名发生变化 则修改评论信息
            List<Comment> commentList=commentMapper.getByUserId(userDTO.getId());
            if(commentList!=null&&commentList.size()>0){
                commentList.forEach(comment -> {
                    comment.setUsername(userDTO.getUsername());
                    commentMapper.update(comment);
                });
            }
            //3. 如果用户名发生变化 则修改举报信息
            List<Report> reportList=reportMapper.getByUserId(userDTO.getId());
            reportList.forEach(report -> {
                report.setUsername(userDTO.getUsername());
                reportMapper.update(report);
            });
        }
        return userMapper.update(userDTO);
    }

    /**
     * 通过id获取用户信息
     *
     * @param id
     * @return
     */
    @Override
    public User getById(Long id) {
        if(id<0)
            return null;
        return userMapper.getById(id);
    }
}
